const Tree = {
  nodeData: [],
  pidname: 'pid',
  icon: ['│', '├', '└'],
  nbsp: '\u00a0',
  childrenKey: 'childlist',
  init (nodeData, pidname = null, nbsp = null) {
    this.nodeData = nodeData
    if (pidname !== null && pidname !== undefined) {
      this.pidname = pidname
    }
    if (nbsp !== null && nbsp !== undefined) {
      this.nbsp = nbsp
    }
  },
  getChild (id) {
    const newNodeData = {}
    this.nodeData.forEach((node) => {
      node.id !== undefined && node[this.pidname].toString() === id.toString() && (newNodeData[node.id] = node)
    })
    return newNodeData
  },
  getChildren (id, withself) {
    var newNodeData = []
    this.nodeData.forEach((node) => {
      if (node.id !== undefined) {
        if (node[this.pidname].toString() === id.toString()) {
          newNodeData.push(node)
          newNodeData = newNodeData.concat(this.getChildren(node.id))
        } else if (withself && node.id.toString() === id.toString()) {
          newNodeData.push(node)
        }
      }
    })
    return newNodeData
  },
  getChildrenIds (id, withself) {
    const childrenList = this.getChildren(id, withself)
    const childrenIds = []
    childrenList.forEach((item) => {
      childrenIds.push(item.id)
    })
    return childrenIds
  },
  getParent (id) {
    var pid = 0
    var nodeData = []
    this.nodeData.forEach((item) => {
      if (item.id.toString() === id.toString()) {
        pid = item[this.pidname]
        return pid
      }
    })
    if (pid) {
      this.nodeData.forEach((item) => {
        if (item.id.toString() === pid.toString()) {
          nodeData = item
          return nodeData
        }
      })
    }
    return nodeData
  },
  getParents (id, withself) {
    var pid = 0
    var newNodeData = []
    this.nodeData.forEach((node) => {
      if (node.id !== undefined) {
        if (node.id.toString() === id.toString()) {
          withself && newNodeData.push(node)
          pid = node[this.pidname]
        }
      }
    })
    if (pid) {
      newNodeData = newNodeData.concat(this.getParents(pid, true))
    }
    return newNodeData
  },
  getParentsIds (id, withself) {
    const parentList = this.getParents(id, withself)
    const parentsIds = []
    parentList.forEach((item) => {
      parentsIds.push(item.id)
    })
    return parentsIds
  },
  getTreeArray (id, itemprefix = '') {
    const childs = this.getChild(id)
    var n = 0
    const data = []
    var number = 1
    if (childs) {
      const total = Object.keys(childs).length
      for (const id in childs) {
        const value = childs[id]
        var j = ''
        var k = ''
        if (number === total) {
          j += this.icon[2]
          k = itemprefix ? this.nbsp : ''
        } else {
          j += this.icon[1]
          k = itemprefix ? this.icon[0] : ''
        }
        value['spacer'] = itemprefix ? itemprefix + j : ''
        data[n] = value
        data[n][this.childrenKey] = this.getTreeArray(id, itemprefix + k + this.nbsp)
        n++
        number++
      }
    }
    return data
  },
  getTreeList (treeData, field = 'name') {
    var data = []
    treeData.forEach((item) => {
      const childList = item.childlist ? item.childlist : []
      delete item.childlist
      item[field] = item.spacer + ' ' + item[field]
      item.haschild = childList ? 1 : 0
      if (item.id) {
        data.push(item)
      }
      if (childList) {
        data = data.concat(this.getTreeList(childList, field))
      }
    })
    return data
  }
}

export default Tree
